"use strict";
/* Controllers */
app.controller("userGroupController", ['$scope', '$location', '$rootScope', '$routeParams', 'userGroupService', function ($scope, $location, $rootScope, $routeParams, userGroupService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listUserGroup = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {};
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        userGroupService.listUserGroup(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $resultTable.bootstrapTable('hideColumn', 'createTime');

        $scope.listUserGroup();
    };

    $scope.initializeController();


    $scope.confirmDel=function(id,index){
            vm.toDelId=id
            vm.toDelIndex=index
            $('#myModal').modal('show')
        }

    $scope.ok=function(){
        var vm = $scope.vm;
        console.log("toDelId"+vm.toDelId)
        console.log("toDelIndex"+vm.toDelIndex)
        var params = {};
        params.id=vm.toDelId
        var procFn = function (response) {
            if (response.success && response.data) {
               $('#list_table').bootstrapTable('remove', {
                   field:'id',
                   values:[vm.toDelId]
               });
               alert("删除成功！")
            } else {
               alert(response.msg);
            }
        };
        userGroupService.delUserGroup(params, procFn, errorFn);
        $('#myModal').modal('hide')
    }

}]);

function userGroupIDFmt(value, row, index) {
        var delCont='<a type="button" class="btn btn-xs btn-danger" onclick=angular.element(this).scope().confirmDel('+ row.id +','+index+') >删除</a>\n'
        var upCont='<a type="button" class="btn btn-xs btn-primary" href="#/star/user/user_group_add/'+row.id+'" target="_blank">修改</a>\n'
        var addRole='<a type="button" class="btn btn-xs btn-primary" href="#/star/user/user_group_role/'+row.id+'" target="_blank">授权</a>'
        return [
            delCont,upCont,addRole
        ].join('');
}


/* Services */
app.service('userGroupService', ['ajaxService', function (ajaxService) {

    this.listUserGroup = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/userGroup/listUserGroup.do", successFunction, errorFunction);
    };

    this.delUserGroup = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/userGroup/delUserGroup.do", successFunction, errorFunction);
    };
}]);

function roleListFmt(value, row, index) {
    if(value==null) {
        return '-';
    }
    var roles = new Set();
    var arr = value.split(',');
    for(var i=0; i<arr.length; ++i) {
        roles.add(arr[i]);
    }
    return Array.from(roles).join('<br/>');
}

function userNamesFmt(value, row, index) {
    if(value==null) {
        return '-';
    }
    var arr = value.split(',');
    return arr.join('<br/>');
}

